home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
MPW_TOOL
/
TOOLS
/
TOOLS_WI
/
ICON_8
/
MEMMON_F
/
MMPS.C
< prev
next >
Wrap
Text File
|
1990-03-02
|
8KB
|
273 lines
/*
* mmps.c: batch mode PostScript translator.
*
* We define and use four general procedures:
* oldy oldx y x n -R- y x+n+1 % n by H rectangle at (x,y), set x & y
* oldy oldx n -A- oldy oldx+n+1 % n by H rectangle adjacent, update x
* oldy oldx -V- oldy oldx % 1 by H rectangle at left, no change
* text x y -S- -- % text string at (x,y)
* We also define a Cnn procedure for each different color used.
*
* "S" is straightforward and simply displays a text string. The other three
* general procedures work together to fill rectangles. They maintain current
* y and x values atop the PostScript stack. x is atop y for easiest access.
*
* "R" draws an n-wide block at (x,y), leaving (x+n+1, y) on the stack. The +1
* allows for the usual one-pixel gap between blocks on the display.
*
* "A" draws an n-wide block adjacent to the last block, updating the stack
* similarly.
*
* "V" draws a 1-wide block to the LEFT of the stack location, leaving it
* unchanged. This is designed for drawing string-region separators.
*/
#include "memmon.h"
hidden novalue rect Params((int c, int x, int y, int n));
hidden novalue pscolor Params((int c));
#define PageHeight 792 /* default dimensions */
#define PageWidth 612
#define SideMargin 72 /* 1" margin at top and sides */
#define TopMargin 72
#define BotMargin 96 /* larger at bottom for QMS color ptr */
#define MaxHeight (PageHeight - TopMargin - BotMargin)
#define MaxWidth (PageWidth - 2 * SideMargin)
#define FrameWidth 1 /* frame surrounding dimensions above */
#define Font "Helvetica"
#define CharHeight (3 * textrow / 4) /* height of a text char */
#define CharBase (textrow / 4) /* offset to baseline within text box */
static int llx, lly, urx, ury; /* bounding box including frame */
static int xcur, ycur; /* current y, x on PostScript stack */
static float psscale; /* output scaling */
static int lastpscolor = -1; /* last color command output */
/*
* devsetup() - set globals to device-dependent values.
*/
novalue devsetup()
{
granularity = 4;
width = MaxWidth;
height = MaxHeight;
textrow = 11;
textsep = 2;
memrow = 20;
batchmode = 1;
}
/*
* devinit() - initialize output file.
*/
novalue devinit()
{
char time_buf[26];
int fwidth, fheight, i;
fheight = height + 2 * FrameWidth; /* dimensions including frame */
fwidth = width + 2 * FrameWidth;
psscale = 1.0;
if (width > MaxWidth)
psscale = (MaxWidth) / (float)width;
if (psscale * height > MaxHeight)
psscale *= (MaxHeight) / (psscale * height);
llx = (PageWidth - psscale * fwidth) / 2;
lly = PageHeight - TopMargin - psscale * fheight;
urx = llx + psscale * fwidth + 0.999;
ury = lly + psscale * fheight + 0.999;
getctime(time_buf);
printf("%%!PS-Adobe-2.0 EPSF-1.2\n");
if (title)
printf("%%%%Title: %s\n", title);
printf("%%%%Creator: mmps\n");
printf("%%%%CreationDate: %s", time_buf);
printf("%%%%DocumentFonts: %s\n", Font);
printf("%%%%BoundingBox: %d %d %d %d\n", llx, lly, urx, ury);
printf("%%%%EndComments\n");
/*
* define general-purpose PostScript procedures
*/
printf("/R { 5 3 roll pop pop 3 copy add exch moveto 0 H rlineto\n");
printf(" dup neg 0 rlineto 0 H neg rlineto fill add 1 add } bind def\n");
printf("/A { 3 copy add exch moveto 0 H rlineto\n");
printf(" dup neg 0 rlineto 0 H neg rlineto fill add 1 add } bind def\n");
printf("/V { 2 copy exch moveto 0 H rlineto\n");
printf(" -1 0 rlineto 0 H neg rlineto fill } bind def\n");
printf("/S { moveto show } bind def\n");
/*
* define a procedure for each unique color
*/
for (i = 0; i < NColors; i++)
if (cmap[i].uniq == i) /* if first occurrence */
printf("/C%d { %.2f %.2f %.2f setrgbcolor } bind def\n", i,
cmap[i].red / 255.0, cmap[i].green / 255.0, cmap[i].blue / 255.0);
printf("%%%%EndProlog\n");
}
/*
* batbegin() - prepare to write an image.
*/
novalue batbegin()
{
static int pageno = 0;
int x;
float yscale;
++pageno;
lastpscolor = -1;
/* initialize for a new page */
printf("%%%%Page: %d %d\n", pageno, pageno);
printf("save\n");
if (sfreq != 0)
printf("currentscreen 3 -1 roll pop %d 3 1 roll setscreen\n", sfreq);
/* scale according to global parameters set up earlier */
printf("%d %d translate\n", llx, lly);
printf("%f %f scale\n", psscale, psscale);
printf("%d %d translate\n", FrameWidth, FrameWidth); /* move inside border */
/* tweak the scaling a little more to fill up unused space at the bottom */
yscale = (float)(height + 2 * FrameWidth) / (height - ymin + 2 * FrameWidth);
printf("0 %d translate\n", height + FrameWidth);
printf("1.0 %f scale\n", yscale);
printf("0 %d translate\n", -height - FrameWidth);
/* draw the background, a rectangle with the last partial line reomved */
pscolor(C_Background);
printf("\n");
x = mempixels % width;
if (x == 0)
x = width;
printf("%d %d moveto %d %d lineto %d %d lineto %d %d lineto\n",
-FrameWidth, ymin - FrameWidth,
-FrameWidth, height + FrameWidth,
width + FrameWidth, height + FrameWidth,
width + FrameWidth, ymin - FrameWidth + memrow);
printf(" %d %d lineto %d %d lineto fill\n",
x + FrameWidth, ymin - FrameWidth + memrow,
x + FrameWidth, ymin - FrameWidth);
/* finish initialization */
printf("1 setlinewidth\n");
if (textrow > 0) {
printf("/%s findfont [%.3f 0 0 %.3f 0 0] makefont setfont\n", Font,
1.9 * (float)width / (float)TextLength, (float)CharHeight);
printf("/H %d def\n", textrow - 1);
}
printf("0 0\n"); /* initial y,x for the PostScript stack */
xcur = ycur = 0;
}
/*
* battext(s, row, col, fg, bg) - output text string.
*/
novalue battext(s, row, col, fg, bg)
char *s;
int row, col, fg, bg;
{
float charwidth;
int x, xx, y;
charwidth = (float)width / (float)TextLength;
x = charwidth * col;
xx = charwidth * (col + strlen(s) + 1);
y = height - (row + 1) * textrow + 1;
rect(bg, x, y, xx - x - 1);
pscolor(fg);
pstext(s);
printf(" %d %d S\n", (int)(x + charwidth / 2), y + CharBase);
}
/*
* batmem(mbuf) - output memory image.
*/
novalue batmem(mbuf)
unsigned char mbuf[];
{
int c, d, n, x, y;
unsigned char *p, *endrow, *endmem;
if (memrow > 1)
printf("/H %d def\n", memrow - 1); /* define row height */
else
printf("/H 1 def\n");
/*
* identify contiguous blocks of color
* (assumes we can skip the separator pixel between two blocks)
*/
p = mbuf;
endmem = mbuf + mempixels;
y = memheight;
while (p < endmem) {
endrow = p + width;
if (endrow > endmem)
endrow = endmem;
x = -1;
y -= memrow;
while (p < endrow) {
c = *p++;
x++;
if (c == C_Bsep)
continue;
n = 1;
while (p < endrow && (d = *p++) == c)
n++;
rect(c, x, y, n); /* fill rectangle with color c */
x += n;
if (d != C_Bsep && p < endrow)
p--, x--;
}
}
printf("pop pop restore showpage\n");
}
/*
* batterm() - terminate entire run.
*/
novalue batterm()
{
printf("%%%%Trailer\n");
}
/*
* rect(c,x,y,n) - fill an H by n rectangle at (x,y) with color c.
*/
static novalue rect(c, x, y, n)
int c, x, y, n;
{
pscolor(c);
if ((n == 1) && (x == xcur - 1) && (y == ycur))
printf("V\n");
else if ((x == xcur) && (y == ycur)) {
printf("%d A\n", n);
xcur = x + n + 1;
}
else {
printf("%d %d %d R\n", y, x, n);
xcur = x + n + 1;
ycur = y;
}
}
/*
* pscolor(c) - write a setcolor command for color c,
* if it's not the same as the last one.
*/
static novalue pscolor(c)
int c;
{
c = cmap[c].uniq;
if (lastpscolor == c)
return;
printf("C%d ", c);
lastpscolor = c;
}